use proc_macro2::TokenStream as T2;
use quote::{format_ident, quote};

pub mod gpio;
mod rcc;
pub mod dma;
pub mod uart;
pub mod adc;

pub fn steal_periph(name: &str) -> T2 {

    let n = format_ident!("{}", name);
    quote! {
        unsafe { export::device::#n::steal() }
    }
}

#[derive(Debug)]
pub struct DmaInfo {
    pub dma: u8,
    pub ch: u8,
    pub stream: u8,
}

impl DmaInfo {

    pub fn find_dma(name: &str) -> Option<Self> {
        match name {
            "USART1_TX" => Some(DmaInfo {
                dma: 2,
                ch: 4,
                stream: 7,
            }),
            "USART1_RX" => Some(DmaInfo {
                dma: 2,
                ch: 4,
                stream: 5,
            }),

            "USART2_TX" => Some(DmaInfo {
                dma: 1,
                ch: 4,
                stream: 6,
            }),
            "USART2_RX" => Some(DmaInfo {
                dma: 1,
                ch: 4,
                stream: 5,
            }),

            "USART3_TX" => Some(DmaInfo {
                dma: 1,
                ch: 4,
                stream: 3,
            }),
            "USART3_RX" => Some(DmaInfo {
                dma: 1,
                ch: 4,
                stream: 1,
            }),

            "UART4_TX" => Some(DmaInfo {
                dma: 1,
                ch: 4,
                stream: 4,
            }),
            "UART4_RX" => Some(DmaInfo {
                dma: 1,
                ch: 4,
                stream: 2,
            }),

            "UART5_TX" => Some(DmaInfo {
                dma: 1,
                ch: 4,
                stream: 7,
            }),
            "UART5_RX" => Some(DmaInfo {
                dma: 1,
                ch: 4,
                stream: 0,
            }),

            "USART6_TX" => Some(DmaInfo {
                dma: 2,
                ch: 5,
                stream: 6,
            }),
            "USART6_RX" => Some(DmaInfo {
                dma: 2,
                ch: 5,
                stream: 1,
            }),

            "UART7_TX" => Some(DmaInfo {
                dma: 1,
                ch: 5,
                stream: 1,
            }),
            "UART7_RX" => Some(DmaInfo {
                dma: 1,
                ch: 5,
                stream: 3,
            }),

            "UART8_TX" => Some(DmaInfo {
                dma: 1,
                ch: 5,
                stream: 0,
            }),
            "UART8_RX" => Some(DmaInfo {
                dma: 1,
                ch: 5,
                stream: 6,
            }),

            "ADC1" => Some(DmaInfo {
                dma: 2,
                ch: 0,
                stream: 0,
            }),
            "ADC2" => Some(DmaInfo {
                dma: 2,
                ch: 1,
                stream: 2,
            }),
            "ADC3" => Some(DmaInfo {
                dma: 2,
                ch: 2,
                stream: 1,
            }),

            _ => None,
        }
    }
}
